import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { LoginData, LoginResult, VerifyCodeResult, TenantInfo } from './types';
import { UserInfo } from '@/api/system/user/types';

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;

/**
 * @param data {LoginData}
 * @returns
 */
export function login(data: LoginData): AxiosPromise<LoginResult> {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || 'password'
  };
  return request({
    url: '/auth/login',
    headers: {
      isToken: false
      // isEncrypt: true
    },
    method: 'post',
    data: params
  });
}

// 注册方法
export function register(data: any) {
  const params = {
    ...data,
    clientId: clientId,
    grantType: 'password'
  };
  return request({
    url: '/auth/register',
    headers: {
      isToken: false
      // isEncrypt: true
    },
    method: 'post',
    data: params
  });
}

/**
 * 注销
 */
export function logout() {
  return request({
    url: '/auth/logout',
    method: 'post'
  });
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return request({
    url: '/auth/code',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  });
}

/**
 * 第三方登录
 */
export function callback(data: LoginData): AxiosPromise<any> {
  const LoginData = {
    ...data,
    clientId: clientId,
    grantType: 'social'
  };
  return request({
    url: '/auth/social/callback',
    method: 'post',
    data: LoginData
  });
}

// 获取用户详细信息
export function getInfo(): AxiosPromise<UserInfo> {
  return request({
    url: '/system/user/getInfo',
    method: 'get'
  });
}

// 获取租户列表
export function getTenantList(): AxiosPromise<TenantInfo> {
  return request({
    url: '/auth/tenant/list',
    headers: {
      isToken: false
    },
    method: 'get'
  });
}

/**
 * 忘记密码
 * @param data
 */
export const forgetPassword = (data) => {
  return request({
    url: '/system/user/forgetPassword',
    method: 'put',
    headers: {
      // isEncrypt: true
    },
    data: data
  });
};
/**
 * 注册时校验手机号是否已被注册
 */
export const registerCheckPhone = (params) => {
  return request({
    url: `/auth/registerCheckPhone/${params}`,
    method: 'get'
  });
};

export const getAllTemplateIds = (params) => {
  return request({
    url: `/auth/getAllTemplateIds`,
    method: 'get',
    params
  });
};
/**
 * 短信验证码（需要图形验证码）
 */
export const sendAliyun = (data) => {
  return request({
    url: `/auth/sendAliyun`,
    method: 'post',
    data
  });
};
/**
 * 短信验证码
 */
export const sendAliyunNoUUid = (data) => {
  return request({
    url: `/auth/sendAliyunNoUUid`,
    method: 'post',
    data
  });
};
/**
 * 未登录状态获取协议内容
 */
export const lastVersion = (params) => {
  return request({
    url: `/logic/dist/lastVersion`,
    method: 'get',
    params
  });
};
